/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AdminManagerment.jdbc;

import AdminManagerment.entities.Staff;
import connection.Configuration;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Tngoc
 */
public class Staffs {

    private ArrayList<Staff> staff;
    public static final int INTERVIEWER = 3;
    public static final int SCHEDULER = 2;
    public static final int ALL = 4;

    public Staffs() {
        this.staff = new ArrayList<>();
        getInfomation();
    }

    

    private void refresh() {
        staff.clear();
        getInfomation();
    }

    //get infomation of staff table in database and add them into the list
    private void getInfomation() {
        try {
            try (Connection conn = new Configuration().getConnect(); Statement st = conn.createStatement()) {
                ResultSet rs = st.executeQuery("select * from Staff");
                //add all the record of staff table into staff
                while (rs.next()) {
                    Staff temp = new Staff();
                    temp.setID(rs.getInt(1));
                    temp.setName(rs.getString(2));
                    temp.setPassword(rs.getString(3));
                    temp.setRole(rs.getInt(4));
                    temp.setEmail(rs.getString("email"));
                    temp.setAddress(rs.getString("address"));
                    temp.setGender(rs.getBoolean("gender"));
                    temp.setActive(rs.getBoolean(5));
                    if (temp.getRole() == 1) {
                        //if staff is admin then skip add
                        continue;
                    }
                    staff.add(temp);
                }
                rs.close();
                conn.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(Staffs.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public ArrayList<Staff> getStaff() {
        refresh();
        return staff;
    }

    //get staff by type ( type is role of staff, type = 4 mean get all staff )
    public ArrayList<Staff> getStaff(int type) {
        refresh();
        if (type == 4) {
            return staff;
        }
        ArrayList<Staff> temp = new ArrayList<>();
        if (type == 5) {
            for (Staff staff1 : staff) {
                if (staff1.isActive() == false) {
                    temp.add(staff1);
                }
            }
            return temp;
        }
        if (type == 6) {
            for (Staff staff1 : staff) {
                if (staff1.isActive() == true) {
                    temp.add(staff1);
                }
            }
            return temp;
        }
        for (Staff staff1 : staff) {
            if (staff1.getRole() == type) {
                temp.add(staff1);
            }
        }
        return temp;
    }

    public void setStaff(ArrayList<Staff> staff) {
        this.staff = staff;
    }

    public void updateStaffStatus(String name) {
        Staff temp = getByName(name);
        String stt;
        if (!temp.isActive()) {
            stt = "1";
        } else {
            stt = "0";
        }
        if (temp != null) {
            try {
                Statement st = new Configuration().getConnect().createStatement();
                st.execute("update Staff set status = " + stt + " where id = " + temp.getID());
                st.getConnection().close();
            } catch (SQLException ex) {
                Logger.getLogger(Staffs.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private Staff getByName(String name)//<editor-fold defaultstate="collapsed" desc="get by name">
    {
        for (Staff staff1 : staff) {
            if (staff1.getName().equals(name)) {
                return staff1;
            }
        }
        return null;
    }
    //</editor-fold>

    public boolean insertStaff(String name, String password, String role,String email,String address,boolean gender) {
        if (!checkName(name)) {
            return false;
        }
        int stRole;
        if ("Scheduler".equals(role)) {
            stRole = 2;
        } else {
            stRole = 3;
        }
        int gd;
        if(gender){
            gd=1;
        }else{
            gd=0;
        }
        try {
            Statement st = new Configuration().getConnect().createStatement();
            st.execute("insert into Staff values('" + name + "','" + password + "'," + stRole + ",1,'"+email+"','"+address+"',"+gd+")");
            st.getConnection().close();
        } catch (SQLException ex) {
            Logger.getLogger(Staffs.class.getName()).log(Level.SEVERE, null, ex);
        }

        return true;
    }

    public boolean updateStaff(String name, String newName, String strRole,String email,String address,boolean gender) {
        if (!name.equals(newName)) {
            if (!checkName(newName)) {
                return false;
            }
        }
        Staff temp = getByName(name);
        int role;
        if ("Scheduler".equals(strRole)) {
            role = 2;
        } else {
            role = 3;
        }
        int gd;
        if(gender){
            gd=1;
        }else{
            gd=0;
        }
        if (temp != null) {
            try {
                Statement st = new Configuration().getConnect().createStatement();
                st.execute("update Staff set role = " + role + ",userName='" + newName + "',email='"+email+"',address='"+address+"',gender="+gd+" where id = " + temp.getID());
                st.getConnection().close();
            } catch (SQLException ex) {
                Logger.getLogger(Staffs.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return true;
    }

    public void updateAdminPass(String pass) {
        try {
            Statement st = new Configuration().getConnect().createStatement();
            st.execute("update Staff set password = '" + pass + "' where userName = 'admin'");
            st.getConnection().close();
        } catch (SQLException ex) {
            Logger.getLogger(Staffs.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public boolean checkName(String name) {
        getInfomation();
        for (Staff staff1 : staff) {
            if (staff1.getName().equals(name) || "admin".equals(name)) {
                return false;
            }
        }
        return true;
    }
}
